/*
 * ParserHelper.java
 *
 * Copyright (C) 2013 UPOU.
 * IS214 - Programming Languages
 * All Rights Reserved.
 *
 * @author Mary Joy Pascual and Simon Zagado
 */

package com.upittc.flopol.core.helper;

import java.awt.Color;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JOptionPane;

import com.upittc.flopol.constant.ErrorConstant;
import com.upittc.flopol.constant.FlopolConstant;
import com.upittc.flopol.core.beans.Symbol;
import com.upittc.flopol.core.beans.Token;
import com.upittc.flopol.gui.FramePanel;

public class CoreHelper {

	private static FramePanel panel;
	
	public CoreHelper(FramePanel panel){
		CoreHelper.panel = panel;
	}
	
	//ERROR PRINT
	public static String setError(String error, int lineNo){
		return ErrorConstant.ER_SYNTAX + error + ErrorConstant.ER_LINE + lineNo;
	}
	
	public static String setErrorWoLine(String error){
		return ErrorConstant.ER_SYNTAX + error;
	}
	
	public static String setErrorSem(String error, int lineNo){
		return ErrorConstant.ER_SEMANT + error + ErrorConstant.ER_LINE + lineNo;
	}
	
	public static String setErrorSem(String error, String lexeme){
		return ErrorConstant.ER_SEMANT + error + lexeme;
	}
	
	public static String errorMessage(String errorMessage){
		System.out.println(ErrorConstant.ER_TOKEN + errorMessage);
		return ErrorConstant.ER_EXEC + errorMessage;
	}
	
	public static String errorMessage(String lexeme, int lineNo){
		System.out.println(ErrorConstant.ER_TOKEN + lexeme+ ErrorConstant.ER_LINE + lineNo);
		return ErrorConstant.ER_TOKEN + lexeme+ ErrorConstant.ER_LINE + lineNo;
	}
	
	//OUTPUT PRINT
	public static void print(Token token){
		System.out.println("token: "+token.getToken()+" --- lexeme: "+token.getLexeme()+ " --- lineNo: "+token.getLineNo() );   //TODO
		panel.getTokenTable().addRow(new Object[]{FlopolConstant.tokenMap.get(token.getToken()),token.getLexeme()});
	}
	
	public static void print(Map <String, Symbol> symtbl){
		panel.getSymbolTable().setRowCount(0);
		for (Entry<String, Symbol> sym : symtbl.entrySet()){
			panel.getSymbolTable().addRow(new Object[]{sym.getValue().getVarType(), sym.getValue().getVarName(), sym.getValue().getVarVal()});
		}
	}
	
	public static void print(String message){
		panel.getConsole().setText(message);
	}
	
	public static void give(String text){
		panel.getConsole().append(text);
		panel.getConsole().setForeground(Color.BLACK);
	}
	
	public static void givemore(String text){
		panel.getConsole().append(text+FlopolConstant.NL);
		panel.getConsole().setForeground(Color.BLACK);
	}
	
	//INPUT
	public static String beg(String x){
		return JOptionPane.showInputDialog(FlopolConstant.BEG+FlopolConstant.SP+x);
	}
	
	
	//HELPER
	public static int tokenVal(String type){
		int token = 0;
		if(type.equals(FlopolConstant.BOOL)){
			token = FlopolConstant.BOL_LIT;
		} else if (type.equals(FlopolConstant.STRING)){
			token = FlopolConstant.STR_LIT;
		} else if (type.equals(FlopolConstant.FLOAT)){
			token = FlopolConstant.FLT_LIT;
		}
		return token;
	}
	
	public static String tokenVal(int type){
		String token = null;
		if(type == FlopolConstant.BOL_LIT){
			token = FlopolConstant.BOOL;
		} else if (type == FlopolConstant.STR_LIT){
			token = FlopolConstant.STRING;
		} else if (type == FlopolConstant.FLT_LIT){
			token = FlopolConstant.FLOAT;
		}
		return token;
	}
	
}
